package service

import (
	"encoding/json"
	"github.com/benji/software/Entities"
	"github.com/benji/software/dao"
	log "github.com/sirupsen/logrus"
)

func GetHistory(id int) (*Entities.HistoryInfo, error) {
	data, err := dao.GetHistoryByID(id)
	if err != nil {
		log.Errorf("Fail to get history data")
		return nil, err
	}
	res := Entities.HistoryInfo{
		ID: data.ID,
	}
	log.Infof("data:%v", data)
	byteCommit := []byte(data.StudentCommit)
	commit := make(map[int]Entities.CommitAnswer, 0)
	err = json.Unmarshal(byteCommit, &commit)
	if err != nil {
		log.Errorf("Fail to marshal studentCommit,err:%v", err)
		return nil, err
	}
	res.CommitAnswer = commit
	return &res, nil
}
func GetUserAllHistory(ID int) ([]*Entities.HistoryBasic, error) {
	data, err := dao.GetAllHistoryByUserID(ID)
	if err != nil {
		log.Errorf("Fail to GetAllHistory: %v", err)
		return nil, err
	}
	res := make([]*Entities.HistoryBasic, 0)
	for _, his := range data {
		temp := &Entities.HistoryBasic{}
		temp.ID = his.ID
		temp.Grades = his.Grades
		temp.WrongAnswer = his.WrongAnswer
		temp.RightAnswer = his.RightAnswer
		res = append(res, temp)
	}
	return res, nil
}
